package com.struts2.student.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.struts2.dao.DaoImpl;
import com.struts2.student.entity.Classes;
import com.struts2.student.entity.Major;
import com.struts2.student.entity.Student;
import com.struts2.util.DBUtilStatement;

public class StudentDaoImpl extends DaoImpl implements StudentDao{
	/**
	 * 查询学生信息
	 */
	public List<Student> findStudent() {
		Connection con = null;
		Statement state = null;
		ResultSet result = null;
		List<Student> list = new ArrayList<Student>();

		DBUtilStatement dbUtil = new DBUtilStatement();
		try {
			con = dbUtil.getConnection();
			state = con.createStatement();
			String sql = "select s.student_id,s.real_name,s.avatar,c.class_name,m.major from student s, classes c, major m where m.major_id=c.class_id";
			result = state.executeQuery(sql);
			if(result!=null){
				while(result.next()){
					Student student = new Student();
					student.setStudentId(result.getInt("student_id"));
					student.setRealName(result.getString("real_name"));
					student.setAvatar(result.getString("avatar"));
					Classes classes = new Classes();
					classes.setClassName(result.getString("class_name"));
					student.setClasses(classes);
					
					Major major = new Major();
					major.setMajor(result.getString("major"));
					classes.setMajor(major);
					
					list.add(student);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} 
		dbUtil.close();
		return list;
	}

}
